<?php

namespace App\Models\Purchase;

use App\Models\Chanjet\TplusExpenseVoucherInfo;
use App\Models\Manger\Manger;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ExpenseVoucherInfoPay extends Model
{
    protected $table = 'expense_voucher_info_pay';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];
    /**
     * 列表
     */
    public static function getList($request, $memberinfo, $isgetall)
    {
        $cpid = $memberinfo['cpid'] ?? 0;
        $query = self::with("approve")->orderBy('create_at', 'desc');
        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }
        if(isset($request->infoId) && $request->infoId > 0){
            $query->where('expense_voucher_info_id', $request->infoId);
        }
        $audit_status = $request->audit_status ?? 0;
        if ($audit_status > 0) {
            $query->where('audit_status', $audit_status);
        }

        $urgent_remark = $request->urgent_remark ?? 0;
        if ($urgent_remark > 0) {
            $query->where('urgent_remark', $urgent_remark);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    public static function getListOne($request, $memberinfo, $isgetall)
    {
        $cpid = $memberinfo['cpid'] ?? 0;
        $manger_id = $memberinfo['id'] ?? 0;

        $query = self::whereHas('approve', function ($q) use ($manger_id) {
            $q->where('approve_id1', $manger_id);
        })->with("approve")->orderBy('create_at', 'desc');

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        $company_name = $request->company_name;
        if($company_name != ''){
            $query->where('company_name','like', '%'.$company_name.'%');
        }

        $expense_type = $request->expense_type;
        if($expense_type != ''){
            $query->where('expense_type', $expense_type);
        }
        $expense_name = $request->expense_name;
        if($expense_name != ''){
            $query->where('expense_name','like', '%'.$expense_name.'%');
        }


        $audit_status = $request->audit_status ?? 0;
        if (!empty($audit_status)) {
            if(!is_array($audit_status)){
                $audit_status = explode(',', $audit_status);
            }
            $query->whereIn('audit_status', $audit_status);
        }

        $urgent_remark = $request->urgent_remark ?? 0;
        if ($urgent_remark > 0) {
            $query->where('urgent_remark', $urgent_remark);
        }

        $start_time = $request->start_time;
        if ($start_time != '') {
            $query->where('create_at', '>=', $start_time);
        }

        $end_time = $request->end_time;
        if ($end_time != '') {
            $query->where('create_at', '<=', $end_time);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public static function getListTwo($request, $memberinfo, $isgetall)
    {
        $cpid = $memberinfo['cpid'] ?? 0;
        $manger_id = $memberinfo['id'] ?? 0;

        $query = self::whereHas('approve', function ($q) use ($manger_id) {
            $q->where('approve_id2',$manger_id)->whereIn('audit_status',['2','3','4']);
        })->with("approve")->orderBy('create_at', 'desc');

        $company_name = $request->company_name;
        if($company_name != ''){
            $query->where('company_name','like', '%'.$company_name.'%');
        }

        $expense_type = $request->expense_type;
        if($expense_type != ''){
            $query->where('expense_type', $expense_type);
        }
        $expense_name = $request->expense_name;
        if($expense_name != ''){
            $query->where('expense_name','like', '%'.$expense_name.'%');
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        $audit_status = $request->audit_status ?? 0;
        if (!empty($audit_status)) {
            if(!is_array($audit_status)){
                $audit_status = explode(',', $audit_status);
            }
            $query->whereIn('audit_status', $audit_status);
        }

        $urgent_remark = $request->urgent_remark ?? 0;
        if ($urgent_remark > 0) {
            $query->where('urgent_remark', $urgent_remark);
        }

        $start_time = $request->start_time;
        if ($start_time != '') {
            $query->where('create_at', '>=', $start_time);
        }

        $end_time = $request->end_time;
        if ($end_time != '') {
            $query->where('create_at', '<=', $end_time);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public static function getListThree($request, $memberinfo, $isgetall)
    {
        $cpid = $memberinfo['cpid'] ?? 0;
        // 20241219 办款不分部门
        // $manger_id = $memberinfo['id'] ?? 0;
        $query = self::whereHas('approve', function ($q){
            $q->whereIn('audit_status',['3']);
        })->with("approve")->with('bankuan')->orderBy('create_at', 'desc');

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        $company_name = $request->company_name;
        if($company_name != ''){
            $query->where('company_name','like', '%'.$company_name.'%');
        }

        $expense_type = $request->expense_type;
        if($expense_type != ''){
            $query->where('expense_type', $expense_type);
        }
        $expense_name = $request->expense_name;
        if($expense_name != ''){
            $query->where('expense_name','like', '%'.$expense_name.'%');
        }
        $urgent_remark = $request->urgent_remark ?? 0;
        if ($urgent_remark > 0) {
            $query->where('urgent_remark', $urgent_remark);
        }

        $start_aomount = $request->start_aomount;
        if ($start_aomount != '') {
            $start_aomount = mathfuncmoney('mul', $start_aomount, 100, 2);
            $query->where('refund_amount', '>=', $start_aomount);
        }

        $end_aomount = $request->end_aomount;
        if ($end_aomount != '') {
            $end_aomount = mathfuncmoney('mul', $end_aomount, 100, 2);
            $query->where('refund_amount', '<=', $end_aomount);
        }

        $pmname = $request->pmname;
        if ($pmname) {
            $query->whereHas('pminfo', function ($query) use ($pmname) {
                $query->where('realname', 'like','%'.$pmname.'%');
            });
        }


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public static function getListFour($request, $memberinfo, $isgetall)
    {
        $cpid = $memberinfo['cpid'] ?? 0;
        // 20241219 办款不分部门
        // $manger_id = $memberinfo['id'] ?? 0;

        $query = self::with("pminfo")->with("bankuan_info")->orderBy('create_at', 'desc')->where('audit_status', '=', 3)->where('bk_status', '=', 2);

        $custom_name = $request->custom_name ?? '';
        if ($custom_name != '') {
            $query->where('custom_name', 'like', '%' . $custom_name . '%');
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        $refund_type = $request->refund_type ?? 0;
        if ($refund_type > 0) {
            $query->where('refund_type', $refund_type);
        }

        $urgent_remark = $request->urgent_remark ?? 0;
        if ($urgent_remark > 0) {
            $query->where('urgent_remark', $urgent_remark);
        }

        $start_aomount = $request->start_aomount;
        if ($start_aomount != '') {
            $start_aomount = mathfuncmoney('mul',$start_aomount,100,2);
            $query->where('refund_amount', '>=', $start_aomount);
        }

        $end_aomount = $request->end_aomount;
        if ($end_aomount != '') {
            $end_aomount = mathfuncmoney('mul',$end_aomount,100,2);
            $query->where('refund_amount', '<=', $end_aomount);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function approve(){
        return $this->hasOne(ExpenseVoucherInfoPayApprove::class, 'expense_voucher_info_pay_id', 'id');
    }

    public function expense_info(){
        return $this->belongsTo(TplusExpenseVoucherInfo::class, 'expense_voucher_info_id', 'id');
    }
    public function bankuan(){
        return $this->hasMany(ExpenseVoucherInfoPayBankuan::class, 'main_id', 'id');
    }
    public function manger(){
        return $this->belongsTo(Manger::class,'manger_id','id');
    }
}
